const usersModel = require('../model/usersManage')
const { getBcryptPwd } = require('../utils/getBcryptPwd')

// 创建用户
const createUser = async (req, res, next) => {
  res.set('Content-Type', 'application/json; charset=utf-8')
  if (req.body.username) {
    const hasThisUser = await usersModel.findOneUser(req.body.username)
    if (!hasThisUser) {
      let bcryptPassword
      if (req.body.password) {
        bcryptPassword = await getBcryptPwd(req.body.password)
      }
      const userInfo = {
        ...req.body,
        password: bcryptPassword
      }
      const result = await usersModel.createUser(userInfo, req.file)
      if (result) {
        res.render('success', {
          data: JSON.stringify({})
        })
      } else {
        res.render('error', {
          data: JSON.stringify("创建失败")
        })
      }
    } else {
      res.render('error', {
        data: JSON.stringify("该用户已存在")
      })
    }
  } else {
    res.render('error', {
      data: JSON.stringify("参数错误")
    })
  }
}

// 删除用户
const deleteUser = async (req, res, next) => {
  res.set('Content-Type', 'application/json; charset=utf-8')
  if (req.query.id) {
    if (req.query.id === '6252c5b2036367c713108e2c') {
      res.render('error', {
        data: JSON.stringify("不能删除 admin 哦")
      })
    } else {
      const result = await usersModel.deleteUser(req.query.id)
      try {
        if (result.deletedCount >= 1) {
          res.render('success', {
            data: JSON.stringify({})
          })
        } else {
          res.render('error', {
            data: JSON.stringify("删除失败")
          })
        }
      } catch (e) {
        res.render('error', {
          data: JSON.stringify("删除失败")
        })
      }
    }
  } else {
    res.render('error', {
      data: JSON.stringify("参数错误")
    })
  }
}

// 修改用户
const editUser = async (req, res, next) => {
  res.set('Content-Type', 'application/json; charset=utf-8')
  if (req.body.id) {
    const bcryptPassword = await getBcryptPwd(req.body.password)
    const userInfo = {
      ...req.body,
      password: bcryptPassword
    }
    const result = await usersModel.editUser(userInfo)
    if (result) {
      res.render('success', {
        data: JSON.stringify({})
      })
    } else {
      res.render('error', {
        data: JSON.stringify("修改失败")
      })
    }
  } else {
    res.render('error', {
      data: JSON.stringify("参数错误")
    })
  }
}

// 获取用户列表
const getUserList = async (req, res, next) => {
  res.set('Content-Type', 'application/json; charset=utf-8')
  const query = {}
  const queryKeys = ['username', 'sex']
  for (const item of queryKeys) {
    if (req.query[item]) {
      query[item] = req.query[item]
    }
  }
  const totalList = await usersModel.getTotal(query)
  const dataList = await usersModel.getUserList(query, req.query)
  res.render('success', {
    data: JSON.stringify( {
      total: totalList.length,
      curPage: req.query.curPage,
      dataList
    })
  })
}

module.exports = {
  createUser,
  deleteUser,
  editUser,
  getUserList,
}
